package com.zbkj.dal.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zbkj.dal.entity.user.UserWeekRecord;
import com.zbkj.dal.pojo.dto.UserOrderItemDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author JinXinyu
 * @date 2024-03-10 12:19:50
 */
public interface UserWeekRecordDao extends BaseMapper<UserWeekRecord> {

    @Select("select count(*) + 1 from eb_user_week_record where reward > #{reward} and start_date >= #{weekStart} and end_date <= #{weekEnd};")
    int rewardRank(@Param("reward") BigDecimal reward, @Param("weekStart") Integer weekStart, @Param("weekEnd") Integer weekEnd);

    @Select("select count(*) + 1 from eb_user_week_record where user_count > #{userCount} and start_date >= #{weekStart} and end_date <= #{weekEnd};")
    int userCountRank(@Param("userCount") Integer userCount, @Param("weekStart") Integer weekStart, @Param("weekEnd") Integer weekEnd);

    @Select("select count(*) + 1 from eb_user_week_record where order_count > #{orderCount} and start_date >= #{weekStart} and end_date <= #{weekEnd};")
    int orderCountRank(@Param("orderCount") Integer orderCount, @Param("weekStart") Integer weekStart, @Param("weekEnd") Integer weekEnd);

    @Select("select count(*) + 1 from eb_user_week_record where order_amount > #{orderAmount} and start_date >= #{weekStart} and end_date <= #{weekEnd};")
    int orderAmountRank(@Param("orderAmount") BigDecimal orderAmount, @Param("weekStart") Integer weekStart, @Param("weekEnd") Integer weekEnd);

    List<UserOrderItemDTO> userSpreadItem(@Param("userIds") List<Integer> userIds);

    @Update("update eb_user_week_record r set r.user_count = r.user_count + 1 where id = #{id};")
    boolean addRecruitNum(@Param("id") Integer id);

    @Update("update eb_user_week_record r set r.reward = r.reward + #{reward} where id = #{id};")
    boolean addRecruitReward(@Param("id") Integer id, @Param("reward") BigDecimal reward);

    @Update("update eb_user_week_record r set r.order_count  = r.order_count + 1, r.order_amount = r.order_amount + #{orderAmount} where id = #{id};")
    boolean addOrderItem(@Param("id") Integer id, @Param("reward") BigDecimal orderAmount);

    @Update("update eb_user_week_record r set r.order_count  = r.order_count - 1, r.order_amount = r.order_amount - #{orderAmount} where id = #{id};")
    boolean cancelOrderItem(@Param("id") Integer id, @Param("reward") BigDecimal orderAmount);

    @Select("select sum(r.reward) from eb_user_week_record r where r.user_id = #{userId} and r.is_deleted = 0;")
    BigDecimal totalReward(@Param("userId")Integer userId);
}
